#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : test.py
# @Author: Richard Chiming Xu
# @Date  : 2025/5/15
# @Desc  : 一次性用dwspark的方式完成所有api调用
import time

from loguru import logger
from dwspark.config import Config
# 加载配置
config = Config()

'''
多语种大模型
'''
# # 模拟问题
# message = [{"role": "system", "content": "あなたはとても専門的な日本語の国語の先生で、文才が華麗です。"},{"role": "user", "content": '100字の作文を書いてください。'}]
# logger.info('----------批式调用对话----------')
# model = MultiLang(config, stream=False)
# logger.info(model.generate(message))
# logger.info('----------流式调用对话----------')
# model = MultiLang(config, stream=True)
# for r in model.generate_stream(message):
#     logger.info(r)
# logger.info('done.')

'''
    语音合成模型
'''
# from dwspark.models import Text2Audio
# text2audio = Text2Audio(config)
# result = text2audio.generate("你好，我的名字叫Amy", "./demo.mp3")
# logger.info(f'-------语音合成结果：{result}-------')

'''
    中英语音识别模型
'''
# from dwspark.models import Audio2Text
# model = Audio2Text(config)
# result = model.recognize('./demo.mp3')
# logger.info(f'识别结果：{result}')

'''
    一句话语音克隆
'''
# from dwspark.models import VoiceSynthesis
# logger.info('----------批式调用----------')
# model = VoiceSynthesis(config, stream=False)
# params_with_training = {
#     'text': "欢迎使用讯飞星火认知大模型。",
#     'model_name': "new_model",
#     'audio_path': './一句话克隆声音/test.wav',
#     # 音频需要宝子们根据要求录制  参见“./dwspark/voice/语音文本.txt”
#     # 'force_train': True,  # Force retraining
#     'output_path': './new_output.mp3',
#     'voice_model_config': 'voice_models.json'  # Add the voice_model_config to params
# }
#
# # Uncomment to run training and synthesis
# result = model.generate(params_with_training)
# logger.info(f"Training and synthesis result: {result}")

'''
    图片生成大模型
'''
# from dwspark.models import Text2Picture
# model = Text2Picture(config)
# img_path = model.generate("请你生成一幅鲸鱼水墨画", "./")
# logger.info(f'生成图片位置：{img_path}')


'''
    图片理解
'''
# from dwspark.models import PictureUnderstanding
# model = PictureUnderstanding(config)
# result = model.understanding("请描述以下这幅画的内容", img_path)
# logger.info(f'理解结果：{result}')

'''
    角色模拟
'''
# from dwspark.models import CharacterSimulator
# simulator = CharacterSimulator(config)
# player_id = simulator.create_player("张三", "张三是一名学生")
# agent_id = simulator.create_agent(player_id, "AI助手")

'''
    知识库服务 TODO 没成功
'''
# from dwspark.models import KnowledgeBase
# model = KnowledgeBase(config)
# # 上传文档
# doc_id = model.upload_document("./LICENSE")
# # 创建知识库
# repo_name = f'测试知识库_{int(time.time())}'
# repo_id = model.create_repository(repo_name, '这是一个测试用的知识库', '测试,知识库')
# # 添加知识库
# time.sleep(24) # 等待
# result = model.add_files_to_repository(repo_id, [doc_id])

'''
    Embedding
'''
# from dwspark.models import LLMEmbedding
# model = LLMEmbedding(config)
# vectors = model.get_embeddings(["你好呀", "我叫amy"])
# logger.info(f'Embedding vectors: {vectors}')

'''
    PPT生成
'''

# from dwspark.models import PPTGenerator
# logger.info("智能PPT生成器初始化成功.")
# ppt_generator = PPTGenerator(config=config)
#
#
# # 根据文本生成PPT
# logger.info("\n--- 演示: 直接根据文本创建PPT ---")
# direct_ppt_query = "请帮我制作一份关于可再生能源的科普PPT，包含太阳能、风能和水能"  # ppt主题
# logger.info(f"PPT主题: \"{direct_ppt_query}\"")
# sid_direct = ppt_generator.create_ppt_from_text(
#     query_text=direct_ppt_query,    # ppt主题
#     template_id="20240718489569D",  # 模板ID
#     author="Datawhale团队", # ppt作者
#     is_card_note=True,
#     ai_image_type="normal" # 使用 "normal" 或 "advanced"
# )
# if sid_direct:
#     logger.info(f"直接文本PPT创建任务已启动，SID: {sid_direct}")
#     ppt_url_direct = ppt_generator.poll_for_result(sid_direct)
#     if ppt_url_direct:
#         logger.info(f"直接文本PPT已生成！下载链接: {ppt_url_direct}")
#
# logger.info("\n--- 智能PPT生成器演示流程结束 ---")

'''
    简历生成
'''
# SDK引入模型
from dwspark.models import ResumeGenerator
import json


resume_gen = ResumeGenerator(config=config)
logger.info("智能简历生成器初始化成功.")

# --- 演示: 生成简历 ---
logger.info("\n--- 演示: 根据文本描述生成智能简历 ---")
# 从原始demo获取的示例描述
description = """姓名：张三，年龄：28岁，教育经历：2018年本科毕业于合肥工业大学；工作经历：java开发工程师..."""
logger.info(f"输入的简历描述: \n{description}")

generated_resume_bytes = resume_gen.generate(resume_description_text=description)

if generated_resume_bytes:
    logger.info(f"智能简历API调用成功，接收到 {len(generated_resume_bytes)} bytes 的数据。")
    response_str = generated_resume_bytes.decode('utf-8')
    response_json = json.loads(response_str)
    logger.info("API响应内容 (格式化):")
    # 使用logger.info打印格式化的JSON，Loguru能很好地处理多行消息
    logger.info(f"\n{json.dumps(response_json, indent=2, ensure_ascii=False)}")
    # 您可以根据需要取消注释以下代码，以提取并单独记录特定字段：
    if "links" in response_json and isinstance(response_json.get("links"), list):
        logger.info("提取到的链接详情:")
        for i, link_info in enumerate(response_json["links"]):
            img_url = link_info.get("img_url")
            word_url = link_info.get("word_url")
            logger.info(f"  链接组 {i+1}:")
            logger.info(f"    图片URL: {img_url}")
            logger.info(f"    文档URL: {word_url}")

logger.info("\n--- 智能简历生成器演示流程结束 ---")






